﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using VCN.App.DataTransferObjects;
using System.Data.SqlClient;

namespace VCN.App.DataAccessLayer
{
    public class RelativeProductDAO:BasicDAO<RelativeProduct>
    {
        protected override DataTable exeSpSelect(string ip_storedProcedure)
        {
            return base.exeSpSelect("sp_tblRelativeProducts_SelectAll");//sp_tblCategories_SelectAll
        }
        public DataTable selectAll2Dt()
        {
            return this.exeSpSelect("");
        }
        protected  DataTable exeSpSelectById(string ip_storeProcedure, int ip_id)
        {
            return base.exeSpSelectById("sp_tblRelativeProducts_SelectRow", ip_id);
        }
        public RelativeProduct SelectById(int ip_id)
        {
            DataTable v_dt = this.exeSpSelectById("", ip_id);
            RelativeProduct v_RelativeProduct = new RelativeProduct();
            v_RelativeProduct.Id = ip_id;
            v_RelativeProduct.Image = v_dt.Rows[0]["RELATIVE_PRODUCT_IMAGE"].ToString();
            v_RelativeProduct.ProductId = int.Parse(v_dt.Rows[0]["PRODUCT_ID"].ToString());
            v_RelativeProduct.Name = v_dt.Rows[0]["RELATIVE_PRODUCT_NAME"].ToString();
            return v_RelativeProduct;
        }

        public override bool insert(RelativeProduct ip_RelativeProduct)
        {
            SqlConnection v_conn = BasicDAO<Product>.GetConnection();
            if (v_conn.State == ConnectionState.Closed)
            {
                v_conn.Open();
            }
            SqlCommand v_command = new SqlCommand("sp_tblRelativeProducts_Insert", v_conn);
            v_command.CommandType = CommandType.StoredProcedure;
            v_command.Parameters.Add("@Image", SqlDbType.VarChar).Value = ip_RelativeProduct.Image;
            v_command.Parameters.Add("@ProductId", SqlDbType.Int).Value = ip_RelativeProduct.ProductId;
            v_command.Parameters.Add("@Name", SqlDbType.NVarChar).Value = ip_RelativeProduct.Name;
            try
            {
                int v_rows = v_command.ExecuteNonQuery();
                v_conn.Close();
            }
            catch (Exception exp)
            {
                v_conn.Close();
                HttpContext.Current.Response.Write(exp.ToString());
                return false;
            }
            return true;


        }
        //public override bool deleteByID(Product ip_product)
        //{
        //    return base.deleteByID(ip_product);
        //}
        public override System.Collections.Generic.List<RelativeProduct> selectAll2List(RelativeProduct ip_obj)
        {
            return null;
        }

        public override bool update(RelativeProduct ip_RelativeProduct)
        {
            SqlConnection v_conn = BasicDAO<Product>.GetConnection();
            if (v_conn.State == ConnectionState.Closed)
            {
                v_conn.Open();
            }
            SqlCommand v_command = new SqlCommand("sp_tblRelativeProducts_Update", v_conn);
            v_command.CommandType = CommandType.StoredProcedure;
            v_command.Parameters.Add("@Id", SqlDbType.Int).Value = ip_RelativeProduct.Id;
            v_command.Parameters.Add("@Image", SqlDbType.VarChar).Value = ip_RelativeProduct.Image;
            v_command.Parameters.Add("@ProductId", SqlDbType.Int).Value = ip_RelativeProduct.ProductId;
            v_command.Parameters.Add("@Name", SqlDbType.NVarChar).Value = ip_RelativeProduct.Name;
            try
            {
                int v_rows = v_command.ExecuteNonQuery();
                v_conn.Close();
            }
            catch (Exception exp)
            {
                v_conn.Close();
                HttpContext.Current.Response.Write(exp.ToString());
                return false;
            }
            return true;


        }
        public bool delete(RelativeProduct ip_RelativeProduct)
        {
            SqlConnection v_conn = BasicDAO<Product>.GetConnection();
            if (v_conn.State == ConnectionState.Closed)
            {
                v_conn.Open();
            }
            SqlCommand v_command = new SqlCommand("sp_tblRelativeProducts_DeleteRow", v_conn);
            v_command.CommandType = CommandType.StoredProcedure;
            v_command.Parameters.Add("@Id", SqlDbType.Int).Value = ip_RelativeProduct.Id;
            try
            {
                int v_rows = v_command.ExecuteNonQuery();
                v_conn.Close();
            }
            catch (Exception exp)
            {
                v_conn.Close();
                HttpContext.Current.Response.Write(exp.ToString());
                return false;
            }
            return true;


        }
        public DataTable SelectByProductId(int ip_ProductId)
        {
            SqlConnection v_conn = BasicDAO<RelativeProduct>.GetConnection();
            if (v_conn.State == ConnectionState.Closed)
            {
                v_conn.Open();
            }
            SqlCommand v_command = new SqlCommand("sp_tblRelativeProducts_SelectProductId", v_conn);
            v_command.CommandType = CommandType.StoredProcedure;
            v_command.Parameters.Add("@ProductId", SqlDbType.Int).Value = ip_ProductId;
            DataTable v_dt = new DataTable();
            SqlDataAdapter v_adap = new SqlDataAdapter(v_command);
            try
            {
                v_adap.Fill(v_dt);
                v_conn.Close();
            }
            catch (Exception exp)
            {
                v_conn.Close();
                HttpContext.Current.Response.Write(exp.ToString());

            }
            return v_dt;


        }
        //protected override DataTable exeSpSelect()
        //{
        //    return base.exeSpSelect("sp_tblProducts_select");
        //}
        public DataTable searchRelativeProduct(int ip_CategoryId,int ip_ProductId)
        {
            SqlConnection v_conn = BasicDAO<RelativeProduct>.GetConnection();
            if (v_conn.State == ConnectionState.Closed)
            {
                v_conn.Open();
            }
            SqlCommand v_command = new SqlCommand("sp_tblRelativeProduct_Search", v_conn);
            v_command.CommandType = CommandType.StoredProcedure;
            v_command.Parameters.Add("@CategoryId", SqlDbType.Int).Value = ip_CategoryId;
            v_command.Parameters.Add("@ProductId", SqlDbType.Int).Value = ip_ProductId;
            DataTable v_dt = new DataTable();
            SqlDataAdapter adapter = new SqlDataAdapter(v_command);
            try
            {

                adapter.Fill(v_dt);
                v_conn.Close();
            }
            catch (Exception exp)
            {
                v_conn.Close();
                HttpContext.Current.Response.Write(exp.ToString());
            }
            return v_dt;
        }
    }
}
